package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.TbAdmin;
import com.dongdongshop.service.TbAdminService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("TbAdmin")
public class TbAdminController {

    //注入service
    @DubboReference
    private TbAdminService tbAdminService;

    //登录;逻辑
    @ResponseBody
    @RequestMapping("login")
    public Result login(TbAdmin tbAdmin){

        //使用shiro编写认证操作
        //获取Subject对象
        Subject subject = SecurityUtils.getSubject();
        //封装用户数据
        UsernamePasswordToken token = new UsernamePasswordToken(tbAdmin.getName(),tbAdmin.getPassword());
        //执行登录方法
        Result result = null;
        try {
            //登录成功
            subject.login(token);
            result = Result.response(ResultEnums.LOGIN_SUCCESS);
        }catch (UnknownAccountException e){
            //用户名不存在
            result = Result.response(ResultEnums.LOGIN_ERRORNAME);
        }catch (IncorrectCredentialsException e){
            result = Result.response(ResultEnums.LOGIN_ERRORPASSWORD);
        }
        return result;
    }
}
